//
//  HelpViewController.m
//  MZJ
//
//  Created by XDD on 16/6/3.
//  Copyright © 2016年 XDD. All rights reserved.
//

#import "HelpViewController.h"
#import "Help_DetailViewController.h"

@interface HelpViewController ()<UIWebViewDelegate,UIScrollViewDelegate,ModelDelegate>

@property (nonatomic, strong) UIWebView *helpItems_webView;
@property (nonatomic, strong) ModelViewController *modelViewController;
@property (nonatomic, strong) JSNetWorkingModel *model;


@end

@implementation HelpViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.topBar.backgroundColor = [UIColor colorWithRed:253/255.0 green:179/255.0 blue:10/255.0 alpha:1];
    self.titleLabel.text = @"救助项目";
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1];
    [self.backButton setTitle:@"" forState:UIControlStateNormal];
    
    _helpItems_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 55, kWidth, kHeight - 55)];
    _helpItems_webView.delegate = self;
    _helpItems_webView.scrollView.delegate = self;
    [self.view addSubview:_helpItems_webView];
    
    if (!_modelViewController) {
        _modelViewController = [[ModelViewController alloc]init];
        [_modelViewController modelUrl_Http_Request:helpItems_model fileName:@"helpItemsModel.txt" webview:_helpItems_webView];
    }
    
    if (!_model) {
        _model = [[JSNetWorkingModel alloc]init];
    }

}

//点击返回键
- (void)clickedBackButton:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView  {
    
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    [dic setObject:[DataModel object].information_arid_Str forKey:@"arid"];
    __weak typeof (self)weakSelf = self;
    [_model setBlockWithReturnBlock:^(id returnValue) {
        
        //        NSLog(@"%@",returnValue);
//        NSString* aStr= [[NSString alloc] initWithData:returnValue encoding:NSUTF8StringEncoding];
//        
//        NSString *sddsd = [NSString stringWithFormat:@"Home.station_news.V.create(%@)",aStr];
//        
//        [weakSelf.helpItems_webView stringByEvaluatingJavaScriptFromString:sddsd];
                [weakSelf.modelViewController JS_datarequestinitWebView:@"Home.station_news.V.create" andData:returnValue andwebView:weakSelf.helpItems_webView];
        
    } WithErrorBlock:^(id errorCode) {
        NSLog(@"%@",errorCode);
    }];
    
    [_model JS_HomeGetHelpItems:nil];
    [GiFHUD dismiss];

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [GiFHUD show];

}

- (BOOL)webView: (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    
    NSLog(@"%@",requestString);
    
    
    //    if (![_modelViewController  intercept_request_HTTP:requestString]) {
    //        NSLog(@"1");
    //        StandViewController *standViewController = [[StandViewController alloc]init];
    //        [self.navigationController pushViewController:standViewController animated:YES];
    ////        [_modelViewController skip_view:@"http://192.168.1.254:8004/pro/Home/station_news.html" andintercept_Str:requestString];
    //    }
    
    if ([requestString rangeOfString:helps_news].location != NSNotFound) {
        
//        NSRange range = [requestString rangeOfString:@"&"];
//        NSString *str = [requestString substringToIndex:range.location];
//        
//        NSRange range1 = [str rangeOfString:@"cid"];
//        NSString *str1 = [str substringFromIndex:range1.location];
//        [DataModel object].rescue_List_Str =  [str1 substringFromIndex:4];
//        
        NSRange range2 = [requestString rangeOfString:@"arid"];
        NSString *str2 = [requestString substringFromIndex:range2.location];
        [DataModel object].helps_News_Str =  [str2 substringFromIndex:5];
//
        
        Help_DetailViewController *help_DetailViewController = [[Help_DetailViewController alloc]init];
        [self.navigationController pushViewController:help_DetailViewController animated:YES];
        //
        return NO;
    }
    
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
